package com.sina.weibo.sdk.net;

import android.content.Context;
import android.text.TextUtils;
import android.util.Pair;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import com.huawei.hms.framework.network.util.HttpUtils;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class ConnectionFactory {
    public static HttpURLConnection createConnect(String str, Context context) {
        if (TextUtils.isEmpty(str) || !(str.startsWith(HttpUtils.HTTP_PREFIX) || str.startsWith("https://"))) {
            throw new RuntimeException("非法url请求");
        }
        HttpURLConnection httpURLConnection;
        try {
            Proxy proxy;
            URL url = new URL(str);
            Pair apn = NetStateManager.getAPN();
            if (apn != null) {
                proxy = new Proxy(Type.HTTP, new InetSocketAddress((String) apn.first, ((Integer) apn.second).intValue()));
            } else {
                proxy = null;
            }
            if (str.startsWith(HttpUtils.HTTP_PREFIX)) {
                if (proxy == null) {
                    httpURLConnection = (HttpURLConnection) url.openConnection();
                } else {
                    httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
                }
            } else if (proxy == null) {
                r0 = (HttpsURLConnection) url.openConnection();
            } else {
                r0 = (HttpsURLConnection) url.openConnection(proxy);
            }
        } catch (MalformedURLException e) {
            httpURLConnection = null;
        } catch (IOException e2) {
            httpURLConnection = null;
        }
        httpURLConnection.setUseCaches(false);
        try {
            httpURLConnection.setRequestMethod("POST");
        } catch (ProtocolException e3) {
        }
        httpURLConnection.setRequestProperty(HttpContants.KEY_CONTENT_TYPE, RequestBody.HEAD_VALUE_CONTENT_TYPE_URLENCODED);
        httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        httpURLConnection.setReadTimeout(20000);
        httpURLConnection.setConnectTimeout(25000);
        return httpURLConnection;
    }
}
